package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.ReturnUUID;
import com.ruoyi.gcgl.domain.GcCustomer;
import com.ruoyi.gcgl.service.IGcCustomerService;

/**
 * 客户管理Controller
 * 
 * @author ruoyi
 * @date 2022-11-27
 */
@Controller
@RequestMapping("/gcgl/customer")
public class GcCustomerController extends BaseController
{
    private String prefix = "gcgl/customer";

    @Autowired
    private IGcCustomerService gcCustomerService;

    @RequiresPermissions("gcgl:customer:view")
    @GetMapping()
    public String customer()
    {
        return prefix + "/customer";
    }

    /**
     * 查询客户管理列表
     */
    @RequiresPermissions("gcgl:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcCustomer gcCustomer)
    {
        startPage();
        gcCustomer.setType(0);//客户、运营合作
        gcCustomer.setDeleteFlag(1);
//        if(getSysUser().isShowRoleIds(getSysUser().getRoleIds())){
//        	gcCustomer.setOwner(getLoginName());
//        }
        List<GcCustomer> list = gcCustomerService.selectGcCustomerList(gcCustomer);
        return getDataTable(list);
    }

    /**
     * 导出客户管理列表
     */
    @RequiresPermissions("gcgl:customer:export")
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcCustomer gcCustomer)
    {
    	gcCustomer.setDeleteFlag(1);
    	gcCustomer.setType(0);//客户、运营合作
        List<GcCustomer> list = gcCustomerService.selectGcCustomerList(gcCustomer);
        ExcelUtil<GcCustomer> util = new ExcelUtil<GcCustomer>(GcCustomer.class);
        return util.exportExcel(list, "客户管理数据");
    }

    /**
     * 新增客户管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap){
    	mmap.put("owner", getLoginName());
    	mmap.put("ownerId", getUserId());
    	mmap.put("deptName", getSysUser().getDept().getDeptName());
    	mmap.put("deptId", getSysUser().getDept().getDeptId());
        return prefix + "/add";
    }

    /**
     * 新增保存客户管理
     */
    @RequiresPermissions("gcgl:customer:add")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GcCustomer gcCustomer){
    	gcCustomer.setType(0);//客户、运营合作
    	gcCustomer.setOrderNo(ReturnUUID.getOrderNo("CS"));
    	gcCustomer.setName(gcCustomer.getCkdw());
    	gcCustomer.setDeleteFlag(1);
    	gcCustomer.setCreateAt(new Date());
    	gcCustomer.setUpdateAt(new Date());
    	gcCustomer.setCreateUser(getLoginName());
    	gcCustomer.setUpdateUser(getLoginName());
        return toAjax(gcCustomerService.insertGcCustomer(gcCustomer));
    }

    /**
     * 修改客户管理
     */
    @RequiresPermissions("gcgl:customer:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcCustomer gcCustomer = gcCustomerService.selectGcCustomerById(id);
        mmap.put("gcCustomer", gcCustomer);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户管理
     */
    @RequiresPermissions("gcgl:customer:edit")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcCustomer gcCustomer)
    {
    	gcCustomer.setType(0);//客户、运营合作
    	gcCustomer.setName(gcCustomer.getCkdw());
        return toAjax(gcCustomerService.updateGcCustomer(gcCustomer));
    }

    /**
     * 删除客户管理
     */
    @RequiresPermissions("gcgl:customer:remove")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcCustomerService.deleteGcCustomerByIds(ids));
    }
    
    @GetMapping("/invokeCustomer")
    public String invokeCustomer(){
        return prefix + "/subCustomer";
    }

    @PostMapping("/invokeCustomerlist")
    @ResponseBody
    public TableDataInfo invokeCustomerlist(GcCustomer gcCustomer)
    {
        startPage();
        gcCustomer.setType(0);//客户、运营合作
        gcCustomer.setDeleteFlag(1);
        List<GcCustomer> list = gcCustomerService.selectGcCustomerList(gcCustomer);
        return getDataTable(list);
    }
}